package net.janeluo.rs.module;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.janeluo.rs.bean.JsonReturnMessage;
import net.janeluo.rs.entity.RS_Shops;
import net.janeluo.rs.service.ShopsManagerService;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * 店铺管理模块
 * 
 * @author DLK)JaneLuo
 * @version 1.0.0
 */
@IocBean
@At("/")
public class ShopManagerModule {
    private static final Log log = Logs.getLog(ShopManagerModule.class);
    
    @Inject
    private ShopsManagerService shopsManagerService;
    
    /**
     * 初始化显示店铺列表
     */
    @At("ShopManager")
    @Ok("jsp:views.ShopManager")
    public List<RS_Shops> initShopManager(HttpServletRequest req, @Param("shopName") String shopName){
        log.debug("-------------- 查询店铺列表开始 ---------------");
        
        List<RS_Shops> list = shopsManagerService.getShopList(shopName);
        req.getSession().setAttribute("menuName", "ShopManager");
        
        log.debug("-------------- 查询店铺列表结束 ---------------");
        return list;
    }
    
    /**
     * 添加店铺
     * 
     * @param shop
     */
    @At("AddShop")
    @Ok("->:/ShopManager.nut")
    public void addShop(@Param("..") RS_Shops shop){
        log.debug("----------- 添加店铺开始 ------------");
        
        shopsManagerService.insertShop(shop);
        
        log.debug("----------- 添加店铺结束 ------------");
    }
    
    /**
     * 店铺更新初始化
     * 
     * @param userid 用户id
     * @return JSON消息实体 
     */
    @At("InitShopUpdate")
    @Ok("json")
    public JsonReturnMessage initShopUpdate(@Param("sid") Integer sid){
        log.debug("--------------- 更新用户初始化开始 ---------------");
        JsonReturnMessage mess = new JsonReturnMessage();
        RS_Shops shop = shopsManagerService.findShopById(sid);
        if (shop != null) {
            mess.setSuccess("0");
            mess.setMessage(shop);
        } else {
            mess.setSuccess("1");
        }
        
        log.debug("--------------- 更新用户初始化结束 ---------------");
        return mess;
    }

    /**
     * 修改店铺信息
     * 
     * @param shop
     */
    @At("UpdateShop")
    @Ok("->:/ShopManager.nut")
    public void updateShop(@Param("..") RS_Shops shop){
        log.debug("----------- 修改店铺信息开始 ------------");
        
        shopsManagerService.updateShop(shop);
        
        log.debug("----------- 修改店铺信息结束 ------------");
    }
    
    /**
     * 删除店铺信息
     */
    @At("DeleteShop")
    @Ok("->:/ShopManager.nut")
    public void deleteShop(@Param("sids") Integer[] sids) {
        log.debug("--------------- 删除店铺信息开始 ----------------");
        
        shopsManagerService.deleteShops(sids);
        
        log.debug("--------------- 删除店铺信息结束 ----------------");
    }
}
